package li.webqq;

import li.http.CookieStore;
import li.webqq.api.CheckRequest;
import li.webqq.api.CheckResponse;
import li.webqq.api.GetFaceRequest;
import li.webqq.api.GetFaceResponse;
import li.webqq.api.GetFriendInfo2Request;
import li.webqq.api.GetFriendInfo2Response;
import li.webqq.api.GetFriendUin2Request;
import li.webqq.api.GetFriendUin2Response;
import li.webqq.api.GetGroupNameListMask2Request;
import li.webqq.api.GetGroupNameListMask2Response;
import li.webqq.api.GetImageRequest;
import li.webqq.api.GetImageResponse;
import li.webqq.api.GetOnlineBuddies2Request;
import li.webqq.api.GetOnlineBuddies2Response;
import li.webqq.api.GetQqLevel2Request;
import li.webqq.api.GetQqLevel2Response;
import li.webqq.api.GetSingleLongNick2Request;
import li.webqq.api.GetSingleLongNick2Response;
import li.webqq.api.GetUserFriends2Request;
import li.webqq.api.GetUserFriends2Response;
import li.webqq.api.Login2Request;
import li.webqq.api.Login2Response;
import li.webqq.api.LoginRequest;
import li.webqq.api.LoginResponse;
import li.webqq.api.Poll2Request;
import li.webqq.api.Poll2Response;
import li.webqq.api.Poll2Response.MessageHandler;
import li.webqq.api.SendBuddyMsg2Request;
import li.webqq.api.SendBuddyMsg2Response;
import li.webqq.api.SendQunMsg2Request;
import li.webqq.api.SendQunMsg2Response;

/**
 * WebQQ所有接口的整合
 */
public class WebQq {
    private String qq;

    private String uid16;

    private String loginUrl;

    private String vfwebqq;

    private String psessionid;

    private CookieStore cookieStore = new CookieStore();

    public void setQq(String qq) {
        this.qq = qq;
    }

    /**
     * 检查账号是否需要验证码
     */
    public CheckResponse check() {
        CheckRequest request = new CheckRequest();
        request.setUin(this.qq);
        CheckResponse response = (CheckResponse) request.execute(cookieStore);
        this.uid16 = response.getUin16();
        return response;
    }

    /**
     * 拉取验证码
     */
    public GetImageResponse getImage() {
        GetImageRequest request = new GetImageRequest();
        request.setUin(this.qq);
        return (GetImageResponse) request.execute(cookieStore);
    }

    /**
     * 验证
     */
    public LoginResponse login(String password, String code) {
        LoginRequest request = new LoginRequest();
        request.setU(this.qq);
        request.setPassword(password);
        request.setUin16(this.uid16);
        request.setVerifycode(code);
        LoginResponse response = (LoginResponse) request.execute(cookieStore);
        this.loginUrl = request.getUrl();// 作为下一步的referer
        return response;
    }

    /**
     * 上线
     */
    public Login2Response login2() {
        Login2Request request = new Login2Request();
        request.setReferer(this.loginUrl);
        Login2Response response = (Login2Response) request.execute(cookieStore);
        this.vfwebqq = response.getVfwebqq();
        this.psessionid = response.getPsessionid();
        return response;
    }

    /**
     * 新线程收信
     */
    public void poll2(final MessageHandler messageHandler) {
        final Poll2Request request = new Poll2Request();
        request.setPsessionid(this.psessionid);
        new Thread() {
            public void run() {
                while (true) {
                    Poll2Response response = (Poll2Response) request.execute(cookieStore);
                    messageHandler.hand(response);
                }
            };
        }.start();
    }

    /**
     * 发好友消息
     */
    public SendBuddyMsg2Response sendBuddyMsg2(String to, String message) {
        SendBuddyMsg2Request request = new SendBuddyMsg2Request();
        request.setTo(to);
        request.setMessage(message);
        request.setPsessionid(this.psessionid);
        return (SendBuddyMsg2Response) request.execute(cookieStore);
    }

    /**
     * 发群消息
     */
    public SendQunMsg2Response sendQunMsg2(String to, String message) {
        SendQunMsg2Request request = new SendQunMsg2Request();
        request.setTo(to);
        request.setMessage(message);
        request.setPsessionid(this.psessionid);
        return (SendQunMsg2Response) request.execute(cookieStore);
    }

    /**
     * 获取好友列表
     */
    public GetUserFriends2Response getUserFriends2() {
        GetUserFriends2Request request = new GetUserFriends2Request();
        request.setQq(this.qq);
        request.setVfwebqq(this.vfwebqq);
        return (GetUserFriends2Response) request.execute(cookieStore);
    }

    /**
     * 获取个人信息
     */
    public GetFriendInfo2Response getFriendInfo2(String tuin) {
        GetFriendInfo2Request request = new GetFriendInfo2Request();
        request.setTuin(tuin);
        request.setVfwebqq(this.vfwebqq);
        return (GetFriendInfo2Response) request.execute(cookieStore);
    }

    /**
     * 获取QQ群列表
     */
    public GetGroupNameListMask2Response getGroupNameListMask2() {
        GetGroupNameListMask2Request request = new GetGroupNameListMask2Request();
        request.setVfwebqq(this.vfwebqq);
        return (GetGroupNameListMask2Response) request.execute(cookieStore);
    }

    /**
     * 获取头像
     */
    public GetFaceResponse getFace(String uin) {
        GetFaceRequest request = new GetFaceRequest();
        request.setUin(uin);
        request.setVfwebqq(vfwebqq);
        return (GetFaceResponse) request.execute(cookieStore);
    }

    /**
     * 获取用户信息
     */
    public GetFriendUin2Response getFriendUin2(String tuin) {
        GetFriendUin2Request request = new GetFriendUin2Request();
        request.setTuin(tuin);
        request.setVfwebqq(vfwebqq);
        return (GetFriendUin2Response) request.execute(cookieStore);
    }

    public GetQqLevel2Response getQqLevel2() {
        GetQqLevel2Request request = new GetQqLevel2Request();
        return (GetQqLevel2Response) request.execute(cookieStore);
    }

    public GetSingleLongNick2Response getSingleLongNick2() {
        GetSingleLongNick2Request request = new GetSingleLongNick2Request();
        return (GetSingleLongNick2Response) request.execute(cookieStore);
    }

    public GetOnlineBuddies2Response getOnlineBuddies2() {
        GetOnlineBuddies2Request request = new GetOnlineBuddies2Request();
        request.setPsessionid(psessionid);
        return (GetOnlineBuddies2Response) request.execute(cookieStore);
    }
}